package com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import com.romotc.IpTools;
import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.webqq.SysConstant;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.qqrobot.webqq.qqInterfaceCall.Ptlogin2_Check_InterCall;
import com.romotc.scache.RequestSpy;
/**
 * 解析返回的数据内容
 * @author romotc
 *
 */
public class Ptlogin2_Check_Processor implements DateProcessor {

	/**
	 * 是否需要输入验证码：1 or 0
	 */
	public static final String Key_IsNeedInputVCode = "ptlogin_Check_isNeedVcode";
	/**
	 * 腾讯返回的自动验证码
	 */
	public static final String Key_VCode = "ptlogin_Check_Vcode";
	/**
	 * 尝试过的次数
	 */
	public static final String Key_TryTimes = "ptlogin_Check_TryTimes";
	
	@Override
	public JSONObject processResponse(QQLogedin loginQQ, RequestSpy rqSpy,
			JSONObject reqDataObj) throws Exception {
		//需要验证码:ptui_checkVC('1','a062ff96b78abee8a5c9610f64eb04f881bb931c1ea46faf');
		//不需要验证码:ptui_checkVC('0','!S20');
		String resInfo  = rqSpy.getResBody();
		System.out.println("Ptlogin2_Check_Processor>>" + resInfo);
		
		//解析接口返回的信息 
		String[] date = parseData(resInfo);
		int callResult = Key_CallResult_Error;
		String resultResion = "";
		
		if(date != null && date.length >=2) {
			String isNeed = date[0];
			String vcode = date[1];
			reqDataObj.put(Key_IsNeedInputVCode, isNeed);
			
			//处理验证码
			if(WebQQConstant.Result_Key_PTLOGIN2_CHECK_NEEDVCODE_YES.equals(isNeed)) {
				
				//需要验证码
				vcode = dealVcode(loginQQ, vcode, reqDataObj);
				if(StringUtils.isNotBlank(vcode)) {//识别出来了验证码
					callResult = Key_CallResult_Success;
				}else {
					resultResion = "需要验证码，但未识别到验证码.";
				}
			}else {
				//不需要验证码
				callResult = Key_CallResult_Success;
			}
			
			reqDataObj.put(Key_VCode, vcode);
			System.out.println("Ptlogin2_Check_Processor>>putInTo reqDateObj " + isNeed + "," + vcode);
			
		}
		//接口调用结果
		reqDataObj.put(Key_CallResult, callResult);
		reqDataObj.put(Key_CallResult_Resion, resultResion);
		
		return reqDataObj;
	}

	/**
	 * 根据SysConstant.Vcode_OptionType系统配置对验证码进行处理
	 * @param loginQQ
	 * @param vcode check接口返回的验证码串 即ptui_checkVC('1','a062ff96b78abee8a5c9610f64eb04f881bb931c1ea46faf'); 第二项内容
	 * @param reqDataObj
	 * @return
	 */
	private String dealVcode(QQLogedin loginQQ, String vcode,
			JSONObject reqDataObj) {
		//尝试的次数
		int tryTimes = reqDataObj.optInt(Key_TryTimes,0);
		//超过系统配置的次数不再尝试
		if(tryTimes >= SysConstant.MaxTryTime) {
			return null;
		}
		
		if(SysConstant.Vcode_OptionType == SysConstant.Vcode_OptionType_Skip) {
			//不处理验证码，简单记录一下跳过该号码
			//RomUnDo记录该号码需要验证码
		}else if(SysConstant.Vcode_OptionType == SysConstant.Vcode_OptionType_ChgIp) {
			try {
				IpTools.changeIp();
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//更换IP后重新调用接口
			try {
				//记录尝试的次数
				reqDataObj.put(Key_TryTimes, ++tryTimes);
				reqDataObj.remove(Key_CallResult);
				reqDataObj.remove(Key_CallResult_Resion);
				
				reqDataObj = Ptlogin2_Check_InterCall.getInstance()
							.call(loginQQ, reqDataObj, Ptlogin2_Check_Processor.getInstance());
				
				vcode = reqDataObj.optString(Key_VCode);
				return vcode;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}

	/**
	 * 解析返回信息中的数据
	 * @param resInfo：接口返回的信息
	 * @return String[]:{Result, VCode}
	 * 					 Result:0不需要验证码，Vcode:腾讯返回的自动验证码
	 * 					 Result:1需要验证码，Vcode:未知用途
	 */
	public String[] parseData(String resInfo) {
		Pattern pattern = Pattern.compile(WebQQConstant.Result_Regex_PTLOGIN2_CHECK);
		Matcher m = pattern.matcher(resInfo);
		if(m.matches()) {
			String result = m.group(1);
			String vcode = m.group(2);
			return new String[]{result, vcode};
		}
		
		return null;
	}

	
	
	
	private static Ptlogin2_Check_Processor instance;
	public static synchronized Ptlogin2_Check_Processor getInstance() {
		if(instance == null)
			instance = new Ptlogin2_Check_Processor();
		
		return instance;
	}
	
	public static void main(String[] args) {
//		String resInfo = "ptui_checkVC('1','a062ff96b78abee8a5c9610f64eb04f881bb931c1ea46faf');";
		String resInfo = "ptui_checkVC('0','!S20');";
		String[] date = Ptlogin2_Check_Processor.getInstance().parseData(resInfo);
		if(date == null || date.length < 2)
			System.out.println("null");
		else {
			System.out.println(date[0]);
			System.out.println(date[1]);
		}
	}

}
